package com.wephone.Student.ui;

import java.io.File;
import java.io.FileNotFoundException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.umeng.fb.FeedbackAgent;
import com.wephone.Student.R;
import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.EditMyInfoResponse;
import com.wephone.Student.bean.GroupResponse;
import com.wephone.Student.bean.MyLiuxuequanCountResponse;
import com.wephone.Student.bean.UserInfoResponse;
import com.wephone.Student.bean.data.MyLocationInfo;
import com.wephone.Student.bean.data.UserInfoData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.RoundImageView;
import com.wephone.Student.util.ShareReferenceUtil;
import com.wephone.Student.util.Util;

public class SettingsActivity extends BaseActivity {
	private static long backClickTime = -1;
	private FeedbackAgent agent;
	private File photoSavedFile = null;
	private String photoSavedName = null;
	private RoundImageView userLogo;
	private ImageView userFlagImage;
	public static boolean needRefresh = false;
//	private LinearLayout myTieziLayout;
	private MyLiuxuequanCountResponse res;
	public static View tabRedPointView;
	
	private MyResponseHandler getHandler8(){
		MyResponseHandler handler = new MyResponseHandler(SettingsActivity.this,MyLiuxuequanCountResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);

				if(getDataSuccess){
					res = (MyLiuxuequanCountResponse)response;
					((TextView)findViewById(R.id.liuxuequan_tiezi_count)).setText(res.getData().getCount()+"");
					if(res.getData().getCount()>0){
						findViewById(R.id.liuxuequan_tiezi_count).setVisibility(View.VISIBLE);
						ImageView statusImage = (ImageView)findViewById(R.id.item_liuxuequan_status_logo_image);
						statusImage.setVisibility(View.VISIBLE);
						final UserInfoData userInfo = res.getData().getuInfo();
						String endStr = userInfo.getuIcon().substring(userInfo.getuIcon().lastIndexOf("."));
						imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userInfo.getuIcon().replace(endStr, "_150_150"+endStr), statusImage, options);
					}else{
						findViewById(R.id.liuxuequan_tiezi_count).setVisibility(View.GONE);
						findViewById(R.id.item_liuxuequan_status_logo_image).setVisibility(View.GONE);
						if(tabRedPointView!=null){
							tabRedPointView.setVisibility(View.GONE);
						}
					}
				}
			}
		};
		return handler;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		
		init();
		
		agent = new FeedbackAgent(SettingsActivity.this);
		agent.sync();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode==1){
			if(photoSavedFile!=null && photoSavedFile.exists()){
//				Bitmap bitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(photoSavedFile.getPath()), 300, 300);
				Bitmap bitmap = Util.decodeFile(photoSavedFile.getPath());
				userLogo.setImageBitmap(bitmap);
       		 	Util.saveMyBitmap(bitmap,photoSavedName,getApplicationContext());
       		 	
       		 	uploadUserInfoData();
			}
   	 	}else if(requestCode==2){
	   	 	if(data!=null){
		   	 	Uri selectedImage = data.getData();
		        String[] filePathColumn = {MediaStore.Images.Media.DATA};
		        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
		        cursor.moveToFirst();
		        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
		        String picturePath = cursor.getString(columnIndex);
		        cursor.close();
		        if(!TextUtils.isEmpty(picturePath)){
		        	 File userLogoFile = new File(picturePath);  
		        	 if(userLogoFile.exists()){
		        		 Bitmap bitmap = Util.decodeFile(picturePath);
		        		 userLogo.setImageBitmap(bitmap);
		        		 Util.saveMyBitmap(bitmap,photoSavedName,getApplicationContext());
		        		 
		        		 uploadUserInfoData();
		        	 }
		         }
	   	 	}
   	 	}else if(requestCode==11 && resultCode==11){
	   	 	UserInfoData myInfo = BaseActivity.getMyInfo(getApplicationContext());
			if(myInfo!=null){
				userLogo = (RoundImageView)findViewById(R.id.user_info_detail_user_logo);
				TextView userName = (TextView)findViewById(R.id.user_name_text);
				userName.setText(myInfo.getuName());
				
				//user_info_detail_flag
				Gson gson = new Gson();
				MyLocationInfo locInfo = gson.fromJson(myInfo.getuLocDetail(), MyLocationInfo.class);
				if(locInfo.getType().equals("PlanGoOut")){
					userFlagImage.setImageResource(Util.getCountryFlagSrc("中国 "));
				}else if(locInfo.getType().equals("Returnees")){
					userFlagImage.setImageResource(Util.getCountryFlagSrc(locInfo.getCountry()));
				}else{
					userFlagImage.setImageResource(Util.getCountryFlagSrc(locInfo.getCountry()));
				}
				
				DisplayImageOptions options = new DisplayImageOptions.Builder()
			    .showStubImage(R.drawable.default_user_logo)
			    .showImageOnFail(R.drawable.default_user_logo)
			    .bitmapConfig(Bitmap.Config.ARGB_8888)
			    .cacheInMemory()
			    .cacheOnDisc()
//			    .displayer(new RoundedBitmapDisplayer(1000))
			    .build();
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+myInfo.getuIcon(), userLogo, options);
			}
   	 	}
	}
	
	private void uploadUserInfoData(){
		RequestParams params = new RequestParams();
		params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
		params.put("uName", BaseActivity.getMyInfo(getApplicationContext()).getuName());
		params.put("uBirthday", BaseActivity.getMyInfo(getApplicationContext()).getuBirthday());
		params.put("uAge", BaseActivity.getMyInfo(getApplicationContext()).getuAge());
		params.put("uConstellation", BaseActivity.getMyInfo(getApplicationContext()).getuConstellation());
		params.put("uGender", BaseActivity.getMyInfo(getApplicationContext()).getuGender());
		params.put("uSign", BaseActivity.getMyInfo(getApplicationContext()).getuSign());
		try {
			params.put("uIcon", photoSavedFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		params.put("uGpsX", ShareReferenceUtil.getLocationLatitude(getApplicationContext()));
		params.put("uGpsY", ShareReferenceUtil.getLocationLontitude(getApplicationContext()));
		params.put("uLocType", BaseActivity.getMyInfo(getApplicationContext()).getuLocType());
		params.put("uLocDetail", BaseActivity.getMyInfo(getApplicationContext()).getuLocDetail());
		params.put("uImg1", BaseActivity.getMyInfo(getApplicationContext()).getuImg1());
		params.put("uImg2", BaseActivity.getMyInfo(getApplicationContext()).getuImg2());
		params.put("uImg3", BaseActivity.getMyInfo(getApplicationContext()).getuImg3());
		HttpClient.getInstance().post(Constants.API_USER_EDIT_USER_URL, params, getHandler2());
	}
	
	private MyResponseHandler getHandler2(){
		MyResponseHandler handler = new MyResponseHandler(SettingsActivity.this,EditMyInfoResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);

				if(getDataSuccess){
					EditMyInfoResponse res = (EditMyInfoResponse)response;
					BaseActivity.infoData = res.getData();
					String infoJson = gson.toJson(res.getData());
					ShareReferenceUtil.saveUserInfoJson(getApplicationContext(), infoJson);
					Toast.makeText(getApplicationContext(), "头像设置成功", Toast.LENGTH_SHORT).show();
//					finish();
					if(photoSavedFile!=null && photoSavedFile.exists()){
						photoSavedFile.delete();
					}
				}
			}
		};
		return handler;
	}
	
	private AsyncHttpResponseHandler uidHandler = new AsyncHttpResponseHandler(){
		@Override
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			super.onSuccess(result);
			
//			System.out.println("----on get user info success----"+result);
			if(!TextUtils.isEmpty(result)){
				try{
					Gson gson = new Gson();
					UserInfoResponse response = gson.fromJson(result, UserInfoResponse.class);
					if(response.getErrNo()!=null && response.getErrNo().equals("0")){
						UserInfoData userInfo = response.getData();
						TextView guanzhuText = (TextView)findViewById(R.id.guanzhu_text);
						TextView fensiText = (TextView)findViewById(R.id.fensi_text);
						guanzhuText.setText("关注"+userInfo.getMycare());
						findViewById(R.id.relCareme).setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View arg0) {
								Bundle bundle = new Bundle();
								bundle.putBoolean("isShowCareMe", true);
								bundle.putString("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
								startActivity(new Intent(getApplicationContext(),CareMeOrMyCareListActivity.class).putExtras(bundle));
							}
						});
						
						guanzhuText.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View arg0) {
								Bundle bundle = new Bundle();
								bundle.putBoolean("isShowCareMe", false);
								bundle.putString("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
								startActivity(new Intent(getApplicationContext(),CareMeOrMyCareListActivity.class).putExtras(bundle));
							}
						});
						fensiText.setText("粉丝"+userInfo.getCareme());
						findViewById(R.id.relFans).setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View arg0) {
								Bundle bundle = new Bundle();
								bundle.putBoolean("isShowCareMe", false);
								bundle.putString("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
								startActivity(new Intent(getApplicationContext(),CareMeOrMyCareListActivity.class).putExtras(bundle));
							}
						});
						fensiText.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View arg0) {
								Bundle bundle = new Bundle();
								bundle.putBoolean("isShowCareMe", true);
								bundle.putString("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
								startActivity(new Intent(getApplicationContext(),CareMeOrMyCareListActivity.class).putExtras(bundle));
							}
						});
						((TextView)findViewById(R.id.group_text)).setText("群组"+(userInfo.getMycreate()+userInfo.getMyjoin()));
						findViewById(R.id.relGroup).setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View arg0) {
								Bundle bundle = new Bundle();
								bundle.putString("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
								startActivity(new Intent(getApplicationContext(),MyGroupsActivity.class).putExtras(bundle));
							}
						});
						findViewById(R.id.group_text).setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View arg0) {
								Bundle bundle = new Bundle();
								bundle.putString("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
								startActivity(new Intent(getApplicationContext(),MyGroupsActivity.class).putExtras(bundle));
							}
						});
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	};
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		if(needRefresh){
			needRefresh = false;
			refreshData();
			
			RequestParams params3 = new RequestParams();
			params3.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
			HttpClient.getInstance().get(Constants.API_GET_STUDY_CIRCLE_INFO_URL, params3, getHandler8());
		}
		
		RequestParams params4 = new RequestParams();
		params4.put("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
		HttpClient.getInstance().get(Constants.API_GET_USER_INFO_URL, params4, uidHandler);
		
		getGroupNum();
	}

	private void init(){
		userFlagImage = (ImageView)findViewById(R.id.user_info_detail_flag);
//		myTieziLayout = (LinearLayout)findViewById(R.id.user_info_my_tiezi_layout);
		
		refreshData();
		
		RequestParams params3 = new RequestParams();
		params3.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
		HttpClient.getInstance().get(Constants.API_GET_STUDY_CIRCLE_INFO_URL, params3, getHandler8());
	}

	private void refreshData(){
		TextView cityText = (TextView)findViewById(R.id.user_info_detail_edit_5);
		TextView hometownText = (TextView)findViewById(R.id.user_info_detail_edit_6);
		TextView schoolText = (TextView)findViewById(R.id.user_info_detail_edit_7);
		Gson gson = new Gson();
		MyLocationInfo locInfo = gson.fromJson(getMyInfo(getApplicationContext()).getuLocDetail(), MyLocationInfo.class);
		hometownText.setText(locInfo.getHome());
		if(locInfo.getType().equals("PlanGoOut")){
			schoolText.setText(locInfo.getToschool());
			cityText.setText("中国 "+locInfo.getCity());
		}else if(locInfo.getType().equals("Returnees")){
			if(!TextUtils.isEmpty(locInfo.getSchool2())){
				schoolText.setText(locInfo.getSchool1()+","+locInfo.getSchool2());
			}else{
				schoolText.setText(locInfo.getSchool1());
			}
			cityText.setText(locInfo.getLoc());
		}else{
			schoolText.setText(locInfo.getSchool());
			cityText.setText(locInfo.getCity()+" "+locInfo.getCountry());
		}
		findViewById(R.id.settings_tiezi_layout).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putString("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
				startActivity(new Intent(getApplicationContext(),ZhidaoMyTieziListActivity.class).putExtras(bundle));
			}
		});
		findViewById(R.id.settings_shoucang_layout).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(getApplicationContext(),ZhidaoMyShoucangListActivity.class));
			}
		});
		
		ImageView genderImage = (ImageView)findViewById(R.id.item_gender);
		if(BaseActivity.getMyInfo(getApplicationContext()).getuGender().equals("0")){
			genderImage.setImageResource(R.drawable.online_male);
		}else{
			genderImage.setImageResource(R.drawable.online_female);
		}
		TextView genderText = (TextView)findViewById(R.id.item_gender_text);
		genderText.setText(BaseActivity.getMyInfo(getApplicationContext()).getuAge());
		
		UserInfoData myInfo = BaseActivity.getMyInfo(getApplicationContext());
		if(myInfo!=null){
			userLogo = (RoundImageView)findViewById(R.id.user_info_detail_user_logo);
			TextView userName = (TextView)findViewById(R.id.user_name_text);
			userName.setText(myInfo.getuName());
			TextView signatureText = (TextView)findViewById(R.id.user_info_detail_edit_8);
			signatureText.setText(myInfo.getuSign());
			
			//user_info_detail_flag
//			Gson gson = new Gson();
//			MyLocationInfo locInfo = gson.fromJson(myInfo.getuLocDetail(), MyLocationInfo.class);
			if(locInfo.getType().equals("PlanGoOut")){
				userFlagImage.setImageResource(Util.getCountryFlagSrc("中国 "));
			}else if(locInfo.getType().equals("Returnees")){
				userFlagImage.setImageResource(Util.getCountryFlagSrc(locInfo.getCountry()));
			}else{
				userFlagImage.setImageResource(Util.getCountryFlagSrc(locInfo.getCountry()));
			}
			
			DisplayImageOptions options = new DisplayImageOptions.Builder()
		    .showStubImage(R.drawable.default_user_logo)
		    .showImageOnFail(R.drawable.default_user_logo)
		    .bitmapConfig(Bitmap.Config.ARGB_8888)
		    .cacheInMemory()
		    .cacheOnDisc()
//		    .displayer(new RoundedBitmapDisplayer(1000))
		    .build();
			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+myInfo.getuIcon(), userLogo, options);
			
			userLogo.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					new AlertDialog.Builder(SettingsActivity.this).setTitle("选择头像")
					.setItems(new String[]{"拍照","相册"}, new DialogInterface.OnClickListener(){
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							if(arg1==0){
								File rootFile = new File(Constants.IMAGE_PHOTO_TMP_PATH);
								if(!rootFile.exists()){
									rootFile.mkdirs();
								}
								photoSavedName = "userlogo_"+System.currentTimeMillis()+".jpg";
								photoSavedFile = new File(Constants.IMAGE_PHOTO_TMP_PATH+photoSavedName);
								Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
								intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoSavedFile));
			                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
			                    startActivityForResult(intent, 1);
							}else if(arg1==1){
								photoSavedName = "userlogo_"+System.currentTimeMillis()+".jpg";
								photoSavedFile = new File(Constants.IMAGE_PHOTO_TMP_PATH+photoSavedName);
								Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
								startActivityForResult(i, 2);
							}
						}})
					.setNegativeButton("取消", null).show();
				}
			});
		}
		
		findViewById(R.id.settings_liuxuequan_status_layout).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putInt("newMsgCount", res.getData().getCount());
				bundle.putSerializable("uInfo", res.getData().getuInfo());
				startActivity(new Intent(getApplicationContext(),MyLiuXueQuanDetailActivity.class).putExtras(bundle));
//				findViewById(R.id.liuxuequan_tiezi_count).setVisibility(View.GONE);
//				findViewById(R.id.item_liuxuequan_status_logo_image).setVisibility(View.GONE);
			}
		});
//		getGroupNum();
		
	}
	private void getGroupNum(){
		RequestParams params3 = new RequestParams();
		params3.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
		HttpClient.getInstance().get(Constants.API_GET_CHART_COUNT, params3, new MyResponseHandler(SettingsActivity.this, BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				try {
					JSONObject object = new JSONObject(result);
					JSONObject dataObject = object.getJSONObject("data");
					int mycreate = dataObject.getInt("mycreate");
					int myjoin = dataObject.getInt("myjoin");
					((TextView)findViewById(R.id.group_text)).setText("群组"+(mycreate+myjoin));
					findViewById(R.id.relGroup).setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							Bundle bundle = new Bundle();
							bundle.putString("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
							startActivity(new Intent(getApplicationContext(),MyGroupsActivity.class).putExtras(bundle));
						}
					});
					findViewById(R.id.group_text).setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							Bundle bundle = new Bundle();
							bundle.putString("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
							startActivity(new Intent(getApplicationContext(),MyGroupsActivity.class).putExtras(bundle));
						}
					});
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			@Override
			public void onFailure(Throwable arg0, String result) {
				super.onFailure(arg0,result);
			}
		});
	}
	
//	private void addTieziItemView(ZhidaoAskData askData){
//		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
//		View convertView = getLayoutInflater().inflate(R.layout.include_user_info_detail_my_tiezi_item, null);
//		TextView itemTime = (TextView)convertView.findViewById(R.id.create_time_text);
//		itemTime.setText(sdf.format(new Date(Long.parseLong(askData.getCtime())*1000)));
//		TextView itemContent = (TextView)convertView.findViewById(R.id.item_content_text);
//		itemContent.setText(askData.getTitle());
//		TextView questionAskName = (TextView)convertView.findViewById(R.id.user_name_text);
//		questionAskName.setText(askData.getuInfo().getuName());
//		ImageView itemImage = (ImageView)convertView.findViewById(R.id.question_ask_image);
//		if(!TextUtils.isEmpty(askData.getuInfo().getuIcon())){
//			DisplayImageOptions options = new DisplayImageOptions.Builder()
//	        .showStubImage(R.drawable.default_user_logo)
//	        .showImageOnFail(R.drawable.default_user_logo)
//	        .bitmapConfig(Bitmap.Config.ARGB_8888)
//	        .cacheInMemory()
//	        .cacheOnDisc()
//	        .displayer(new RoundedBitmapDisplayer(1000))
//	        .build();
//			String endStr = askData.getuInfo().getuIcon().substring(askData.getuInfo().getuIcon().lastIndexOf("."));
//			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+askData.getuInfo().getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
//		}else{
//			itemImage.setImageResource(R.drawable.default_user_logo);
//		}
//		convertView.setTag(askData);
//		convertView.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				ZhidaoAskData askData = (ZhidaoAskData)arg0.getTag();
//
//				Bundle bundle = new Bundle();
//				bundle.putSerializable("askData", askData);
//				startActivity(new Intent(getApplicationContext(),ZhidaoAnswerListActivity.class).putExtras(bundle));
//			}
//		});
//		myTieziLayout.addView(convertView);
//	}
	
	private DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.liuxuequan_image_default)
    .showImageOnFail(R.drawable.liuxuequan_image_default)
    .bitmapConfig(Bitmap.Config.ARGB_8888)
    .cacheInMemory()
    .cacheOnDisc()
//    .displayer(new RoundedBitmapDisplayer(1000))
    .build();
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			 long nowTime = System.currentTimeMillis();
				if(backClickTime<=0 || nowTime-backClickTime>2000){
					backClickTime = nowTime;
					Toast.makeText(getApplicationContext(), "再按一次 退出程序", Toast.LENGTH_SHORT).show();
				}else{
					finish();
				}
			return true; 
        }
        return super.onKeyDown(keyCode, event); 
	}
	
}
